package com.mdd.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICustomerService;
import com.mdd.admin.validate.CustomerCreateValidate;
import com.mdd.admin.validate.CustomerUpdateValidate;
import com.mdd.admin.validate.CustomerSearchValidate;
import com.mdd.admin.vo.CustomerListedVo;
import com.mdd.admin.vo.CustomerDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Customer;
import com.mdd.common.mapper.CustomerMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户实现类
 * @author LikeAdmin
 */
@Service
public class CustomerServiceImpl implements ICustomerService {
        
    @Resource
    CustomerMapper customerMapper;

    /**
     * 客户列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerListedVo>
     */
    @Override
    public PageResult<CustomerListedVo> list(PageValidate pageValidate, CustomerSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("customer_number");

        customerMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "=:caseType@case_type:long",
        });

        IPage<Customer> iPage = customerMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerListedVo> list = new LinkedList<>();
        for(Customer item : iPage.getRecords()) {
            CustomerListedVo vo = new CustomerListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Customer
     */
    @Override
    public CustomerDetailVo detail(Integer id) {
        Customer model = customerMapper.selectOne(
                new QueryWrapper<Customer>()
                    .eq("customer_number", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerDetailVo vo = new CustomerDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CustomerCreateValidate createValidate) {
        Customer model = new Customer();
        model.setCustomerName(createValidate.getCustomerName());
        model.setCustomerCall(createValidate.getCustomerCall());
        model.setIdNumber(createValidate.getIdNumber());
        model.setSex(createValidate.getSex());
        model.setAddress(createValidate.getAddress());
        model.setBank(createValidate.getBank());
        model.setAmountDue(createValidate.getAmountDue());
        model.setCaseType(createValidate.getCaseType());
        model.setRemark(createValidate.getRemark());
        model.setCourtClerkId(createValidate.getCourtClerkId());
        String username = LikeAdminThreadLocal.get("username").toString();
        model.setCreationTime(new Date());
        model.setModificationTime(new Date());
        model.setFounder(username);
        model.setModifier(username);
        model.setStatus(1L);
        model.setState(0L);
        model.setMediationState(0L);
        customerMapper.insert(model);
    }

    /**
     * 客户编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CustomerUpdateValidate updateValidate) {
        Customer model = customerMapper.selectOne(
                new QueryWrapper<Customer>()
                    .eq("customer_number",  updateValidate.getCustomerNumber())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setCustomerNumber(updateValidate.getCustomerNumber());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setCustomerCall(updateValidate.getCustomerCall());
        model.setIdNumber(updateValidate.getIdNumber());
        model.setSex(updateValidate.getSex());
        model.setAddress(updateValidate.getAddress());
        model.setBank(updateValidate.getBank());
        model.setAmountDue(updateValidate.getAmountDue());
        model.setCaseType(updateValidate.getCaseType());
        model.setRemark(updateValidate.getRemark());
        model.setCourtClerkId(updateValidate.getCourtClerkId());
        String username = LikeAdminThreadLocal.get("username").toString();
        model.setModifier(username);
        model.setModificationTime(new Date());
        customerMapper.updateById(model);
    }

    /**
     * 客户删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Customer model = customerMapper.selectOne(
                new QueryWrapper<Customer>()
                    .eq("customer_number", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        customerMapper.delete(new QueryWrapper<Customer>().eq("customer_number", id));
    }

}
